package com.mobilepay.moneypig.model.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.activity.search.BankActivity;
import com.mobilepay.moneypig.model.activity.search.ChooseActivity;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.mine.UserDetailData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.PreferenceUtil;

/**
 * Created by xlm on 2017/7/24 0024
 */

public class AddCardActivity extends BaseActivity {
    private static final String TAG = "AddCardActivity";
    private RelativeLayout mRelativeBack;
    EditText mTextProvice;
    EditText mTextCity;
    EditText mTextName;
    private RecyclerView mRecyclerViewAddCard;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_card_add);
    }

    @Override
    protected void initViews() {
        mRecyclerViewAddCard = (RecyclerView) findViewById(R.id.addcard_recyle);
        mRelativeBack = (RelativeLayout) findViewById(R.id.add_back);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        //判断软键盘是否显示
//        IsSoftShow.doMonitorSoftKeyWord(findViewById(R.id.root), new IsSoftShow.OnSoftKeyWordShowListener() {
//            @Override
//            public void hasShow(boolean isShow) {
//                if (isShow) {
//                    StatusBarCompat.setStatusBarColor(AddCardActivity.this, Color.parseColor("#40000000"));
//                } else {
//                    StatusBarCompat.setStatusBarColor(AddCardActivity.this, Color.parseColor("#e46641"));
//                }
//            }
//        });
        AddCardAdapter addCardAdapter = new AddCardAdapter(AddCardActivity.this);
        mRecyclerViewAddCard.setLayoutManager(new LinearLayoutManager(AddCardActivity.this,
                LinearLayoutManager.VERTICAL, false));
        mRecyclerViewAddCard.setAdapter(addCardAdapter);

    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        //移除软键盘监听
//        IsSoftShow.doMonitorSoftKeyWord(findViewById(R.id.root), null);
//    }

    private static final int REQUST_PROVICE = 1;
    private static final int REQUST_CITY = 2;
    private static final int REQUST_NAME = 3;

    private static String pid;
    private static String name;

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    String cardFrom;
    String cardNum;
    String provice;
    String city;
    String bank;
    String mobile;

    private void setListener(final AddCardHolder holder) {
        holder.mEditTextBankProvice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pid = "0";
                Intent intent = new Intent();
                intent.putExtra("pid", pid);
                intent.putExtra("name", "");
                intent.setClass(getThis(), ChooseActivity.class);
                startActivityForResult(intent, REQUST_PROVICE);
            }
        });
        mTextProvice = holder.mEditTextBankProvice;
        holder.mEditTextBankCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.isEmpty(holder.mEditTextBankProvice.getText().toString())) {
                    showToastShort("请选择开户行所在省");
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("pid", pid);
                    intent.putExtra("name", "");
                    intent.setClass(getThis(), ChooseActivity.class);
                    startActivityForResult(intent, REQUST_CITY);
                }
            }
        });
        mTextCity = holder.mEditTextBankCity;
        holder.mEditTextBankName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getThis(), BankActivity.class);
                startActivityForResult(intent, REQUST_NAME);
            }
        });
        mTextName = holder.mEditTextBankName;
    }

    private void checkData(AddCardHolder holder) {
        bank = holder.mEditTextBankName.getText().toString();
        cardNum = holder.mEditTextBankNum.getText().toString().trim();
        mobile = holder.mEditTextBankPhone.getText().toString();
        provice = holder.mEditTextBankProvice.getText().toString();
        city = holder.mEditTextBankCity.getText().toString();
        cardFrom = holder.mEditTextBankAddress.getText().toString().trim();
        if (CommonUtil.isEmpty(bank)) {
            showToastShort("请选择开户行名称");
            return;
        }
        if (CommonUtil.isEmpty(cardNum)) {
            showToastShort("请输入银行卡号");
            return;
        }
        if (CommonUtil.isEmpty(mobile)) {
            showToastShort("请输入银行预留手机号码");
            return;
//            if (CommonUtil.isMobile(mobile)) {
//                showToastShort("请输入银行预留手机号码");
//                return;
//            } else {
//                showToastShort("请输入正确的手机号码");
//                return;
//            }
        }
        if (CommonUtil.isEmpty(provice)) {
            showToastShort("请选择开户行所在省");
            return;
        }
        if (CommonUtil.isEmpty(city)) {
            showToastShort("请选择开户行所在市");
            return;
        }
        if (CommonUtil.isEmpty(cardFrom)) {
            showToastShort("请输入开户行名称");
            return;
        }
        checkCardNum(holder.mButtonCommit);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if ("0".equals(pid)) {
            pid = PreferenceUtil.getParam(getThis(), "pid", "").toString();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUST_PROVICE:
                    pid = data.getStringExtra("pid");
                    name = data.getStringExtra("name");
                    mTextProvice.setText(name);
                    mTextCity.setText(null);
                    break;
                case REQUST_CITY:
                    name = data.getStringExtra("name");
                    mTextCity.setText(name);
                    break;
                case REQUST_NAME:
                    name = data.getStringExtra("name");
                    mTextName.setText(name);
                    break;
            }
        }
    }


    private static class AddCardAdapter extends RecyclerView.Adapter<AddCardHolder> {
        private Context mContext;

        public AddCardAdapter(Context context) {
            mContext = context;
        }


        @Override
        public AddCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new AddCardHolder(LayoutInflater.from(mContext).inflate(R.layout.item_addcard, parent, false));
        }

        @Override
        public void onBindViewHolder(final AddCardHolder holder, int position) {
            ((AddCardActivity) mContext).setListener(holder);
            holder.mButtonCommit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((AddCardActivity) mContext).checkData(holder);
                }
            });
        }

        @Override
        public int getItemCount() {
            return 1;
        }
    }


    static class AddCardHolder extends RecyclerView.ViewHolder {
        EditText mEditTextBankName;
        EditText mEditTextBankNum;
        EditText mEditTextBankPhone;
        EditText mEditTextBankProvice;
        EditText mEditTextBankCity;
        EditText mEditTextBankAddress;
        Button mButtonCommit;

        public AddCardHolder(View itemView) {
            super(itemView);
            mEditTextBankName = (EditText) itemView.findViewById(R.id.card_add_name);
            mEditTextBankNum = (EditText) itemView.findViewById(R.id.card_add_num);
            mEditTextBankPhone = (EditText) itemView.findViewById(R.id.card_add_mobile);
            mEditTextBankProvice = (EditText) itemView.findViewById(R.id.card_add_provice);
            mEditTextBankCity = (EditText) itemView.findViewById(R.id.card_add_city);
            mEditTextBankAddress = (EditText) itemView.findViewById(R.id.card_add_from);
            mButtonCommit = (Button) itemView.findViewById(R.id.card_add_confirm);
        }
    }

    private void checkCardNum(final Button commit) {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.bank_bind);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("bank_card_code", cardNum);
        param.add("bank_branch_name", cardFrom);
        param.add("bank_name", bank);
        param.add("bank_province", provice);
        param.add("bank_city", city);
        param.add("mobile", mobile);
        param.add("sign", param.getMd5String());
        showProgress("处理中");
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissProgress();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 0) {
                        dismissProgress();
                        showToastShort(responseResult.getInfo());
                    } else {
                        showToastShort("绑定成功");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                commit.setClickable(false);
                                getUserDetail();
                            }
                        });
                    }
                }
            }
        });
    }

    //账户信息
    private void getUserDetail() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.get_detail);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissProgress();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissProgress();
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 0) {
                        showToastShort(responseResult.getInfo());
                    } else {
                        UserDetailData data = GsonUtil.GsonToBean(string, UserDetailData.class);
                        App.getApp().setUserDetailInfo(data);//保存账户信息
                        startActivity(BankCardActivity.class, true, true);
                    }
                }
            }
        });
    }

}
